package com.example.f8399.testdemo.activity;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.f8399.testdemo.BaseActivity;
import com.example.f8399.testdemo.R;
import com.example.f8399.testdemo.databinding.ActivityRxBindingBinding;
import com.jakewharton.rxbinding3.view.RxView;
import com.jakewharton.rxbinding3.widget.RxTextView;

import java.util.concurrent.TimeUnit;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import kotlin.Unit;

public class RxBindingActivity extends BaseActivity {

    ActivityRxBindingBinding mBinding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this,R.layout.activity_rx_binding);

        initView();
    }

    private void initView() {
        Disposable btn1Click = RxView.clicks(mBinding.btn1)
                .throttleFirst(5, TimeUnit.SECONDS)//5秒内防止多次点击
                .subscribe(new Consumer<Unit>() {
                    @Override
                    public void accept(Unit unit) throws Exception {
                        LogUtils.d("btn1 on click");
                        ToastUtils.showShort("hello");
                    }
                });

        mCompositeDisposable.add(btn1Click);

        Disposable btn1LongClick = RxView.longClicks(mBinding.btn1)
                .subscribe(new Consumer<Unit>() {
                    @Override
                    public void accept(Unit unit) throws Exception {
                        LogUtils.d("btn1 long click");
                    }
                });

        mCompositeDisposable.add(btn1LongClick);

        Disposable etTextChange = RxTextView.textChanges(mBinding.et1)
                .subscribe(new Consumer<CharSequence>() {
                    @Override
                    public void accept(CharSequence charSequence) throws Exception {
                        mBinding.tv1.setText(charSequence.toString());
                    }
                });
        mCompositeDisposable.add(etTextChange);
    }
}
